<template>
    <div>
        <el-row>
            <el-col :span="16" class="sou">
                <span class="na">创建时间:</span>
                <el-date-picker v-model="time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
            </el-col>
            <el-col :span="2">
                <el-button type="primary" @click="sou">
                    搜索
                </el-button>
            </el-col>
        </el-row>
        <el-row class="res">
            <el-col>
                <el-table :data="list" border style="width: 725px">
                    <el-table-column prop="jid" label="ID" label-class-name="header" align="center" />
                    <el-table-column prop="jname" label="名称" label-class-name="header" align="center" />
                    <el-table-column prop="jtitle" label="标题" label-class-name="header" align="center" />
                    <el-table-column prop="shijian" label="创建时间" label-class-name="header" align="center" />
                    <el-table-column label="回复" label-class-name="header" align="center">
                        <template #default="scope">
                            <el-button type="text" @click="see(scope)">{{ scope.row.num }}</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" label-class-name="header" align="center">
                        <template #default="scope">
                            <el-button type="text" @click="del(scope)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <el-row>
            <el-col>
                <el-pagination layout="prev, pager, next" :total="num" :current-page="pages" @current-change="chan" />
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="3" :offset="21" class="close">
                <el-button @click="quxiao">
                    返回
                </el-button>
            </el-col>
        </el-row>
        <el-dialog v-model="show" :title="title" :class="{small:title=='提示'}">
            <ShanChu :key="codes" :code="codes" v-if="title=='提示'" @tuichu="delt"></ShanChu>
            <HuiFu :key="codes" :code="codes" v-if="title=='查看回复'" @tuichu="huifuout"></HuiFu>
        </el-dialog>
    </div>
</template>

<script setup>
    import { defineEmits, defineProps, onMounted, ref, h } from "vue";
    import axios from "axios";
    import qs from "qs";
    import ShanChu from "./ShanChu.vue";
    import HuiFu from "./HuiFu.vue";
    import { format } from "date-fns";
    import { ElMessage } from "element-plus";

    const emites = defineEmits(["tuichu"]);

    const props = defineProps({
        code: {
            type: Number
        }
    });

    let id = ref(props.code);
    let pages = ref(1);
    let num = ref(1);
    let show = ref(false);
    let title = ref("");
    let list = ref([]);
    let codes = ref();
    let time = ref("");

    const sou = async function () {
        let stime = "";
        let etime = "";
        if (time.value) {
            stime = format(time.value[0], "yyyy-MM-dd") + " 00:00:00";
            etime = format(time.value[1], "yyyy-MM-dd") + " 23:59:59";
        }
        let res = await axios.get(
            `/jiaoliu/show?id=${id.value}&stime=${stime}&etime=${etime}`
        );
        list.value = res.data.data.map((item) => {
            item.shijian = new Date(item.jtime).toLocaleString();
            return item;
        });
    };

    const see = function (data) {
        show.value = true;
        title.value = "查看回复";
        codes.value = data.row.jid;
    };

    const quxiao = function () {
        emites("tuichu");
    };

    const del = function (data) {
        show.value = true;
        title.value = "提示";
        codes = data.row.jid;
    };

    const delt = async function (data) {
        show.value = false;
        if (data) {
            await axios.post(
                "jiaoliu/delete",
                qs.stringify({
                    jId: data.id
                })
            );
            ElMessage({
                message: h(
                    "div",
                    { style: "width:300px;font-size:15px;color:rgb(99, 185, 129)" },
                    "删除成功!"
                ),
                type: "success"
            });
        } else {
            ElMessage({
                message: h(
                    "div",
                    { style: "width:300px;color:#999;font-size:15px" },
                    "已取消删除"
                )
            });
        }
        sou();
    };

    const huifuout = function () {
        show.value = false;
    };

    onMounted(async function () {
        sou();
    });
</script>

<style lang="less" scoped>
    .res {
        margin-top: 20px;
    }

    /deep/ .header {
        background-color: #4f91ff !important;
        color: white;
    }

    .close {
        margin-top: 50px;
        position: relative;
        left: 8px;
    }
</style>